/**
 *  file_manager.cpp
 *  AwkwardPaint - Engine
 *
 *  Date: 10/08/2013
 *
 *  Language: C++
 *
 *  @author Carl McCaffrey
 *
 */
#include "utility/file_manager.h"

const char* FileManager::ReadFile(const char* filename)
{
    std::ifstream file;
    file.open(filename);

    if(!file.is_open()){
        printf("ERROR: Failed to read file: %s!\n", filename);
        return " ";
    }

    std::string contents;
    char line[300];
    while(!file.eof()){
        file.getline(line, 300);
        contents += line;
        contents += "\n";
    }

    return contents.c_str();
}

void FileManager::OpenWriteFile(WriteableFile* file, const char* filename)
{
    file->open(filename);

    if(!file->is_open()){
        printf("ERROR: Failed to open write file: %s!\n", filename);
    }
}

void FileManager::WriteToFile(WriteableFile* file, const char* string)
{
    if(file->is_open()){
        *file << string;
        file->flush();
    }else{
        printf("ERROR: Failed to write to file!\n");
    }
}

void FileManager::CloseWriteFile(WriteableFile* file)
{
    file->flush();
    file->close();
}
